﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using HsTec.Mobile.Green.Util;
using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;



public partial class ReportStorage_ImportReport : System.Web.UI.Page
{
    ReportTemplateBLL bll = new HsTec.Mobile.Green.BLL.ReportTemplateBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            sltYear.Value = DateTime.Now.Year.ToString();
            sltMonth.Value = string.Format("{0:D2}", DateTime.Now.Month);
            bindSlt();
        }
    }
    private void bindSlt()
    {
        List<ReportTemplate> list = bll.GetModelList(" FieldType=0 and FieldTemplate=0");
        foreach (ReportTemplate rt in list)
        {
            ListItem item = new ListItem(rt.FieldAlias, rt.RT_ID.ToString());
            sltTable.Items.Add(item);
        }
    }
    protected void btn_Statistics_Click(object sender, EventArgs e)
    {
        //1.提交前的判断
        if (String.IsNullOrEmpty(sltTable.Value))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择导入数据表！');</script>");
            return;
        }
        //2.验证上传文件类型
        string UpfileExt = System.IO.Path.GetExtension(FU_Statistics.FileName);
        if (UpfileExt != ".xls")
        {
            Text_Confirm_Upstate.Text = "入库的文件名类型不符要求！（必须是EXCEL文件）";
            return;
        }
        //3.文件上传
        switch (Util.Upload_File(FU_Statistics, FU_Statistics.FileName, "Phase2\\" + sltTable.Value))
        {
            case 1:
                Text_Confirm_Upstate.Text = "上传文件成功！";
                break;
            case 0:
                Text_Confirm_Upstate.Text = "上传文件失败！";
                return;
        }
        //4.分析上传文件，并导入数据库
        Text_Confirm_Upstate.Text = String.Empty;
        ReportHelper rhelper = new ReportHelper();
        //指明插入的数据表
        rhelper.tableName = sltTable.Value;
        //指明数据表对应的EXCEL文件
        rhelper.excelPath = "Phase2\\" + rhelper.tableName + "\\" + FU_Statistics.FileName;
        //导入数据日期
        string sDate = sltYear.Value + sltMonth.Value;
        int iEditer = 0;
        Dictionary<string, string> dicRtn = new Dictionary<string, string>();
        Text_Confirm_Upstate.Text = rhelper.importReport(sDate, sltTableType.Value, iEditer, ref dicRtn);
        Text_Confirm_Upstate.Text += "<BR/>" + dicRtn["Rept"];
        Text_Confirm_Upstate.Text += "<BR/>" + dicRtn["Err"];
        Text_Confirm_Upstate.Text += "<BR/>" + dicRtn["NoSheet"];
    }
}
